package it.marcomondini.proximitytools.facades;

import it.marcomondini.proximitytools.MyApplication;
import android.content.Context;
import android.content.SharedPreferences;
import android.location.Location;

public class PreferencesFacade {

    private static final String POINT_LATITUDE_CHECK = "POINT_LATITUDE_CHECK";
    private static final String POINT_LONGITUDE_KEY = "POINT_LONGITUDE_KEY";
    private static final String POINT_LATITUDE_KEY = "POINT_LATITUDE_KEY";
    private static final String REFERENCE_POINT = "REFERENCE_POINT";

    public static final PreferencesFacade preferencesFacade = new PreferencesFacade();
    public static final Context applicationContext = MyApplication.getInstance();

    public static void saveReferencePoint(Location location) {
	SharedPreferences.Editor prefsEditor = applicationContext.getSharedPreferences(REFERENCE_POINT, Context.MODE_PRIVATE).edit();
	prefsEditor.putString(POINT_LATITUDE_CHECK, "OK");
	prefsEditor.putFloat(POINT_LATITUDE_KEY, (float) location.getLatitude());
	prefsEditor.putFloat(POINT_LONGITUDE_KEY, (float) location.getLongitude());
	prefsEditor.apply();
    }

    public static Location getReferencePoint() {
	SharedPreferences prefs = applicationContext.getSharedPreferences(REFERENCE_POINT, Context.MODE_PRIVATE);
	if (prefs.getString(POINT_LATITUDE_CHECK, null) == null) {
	    return null;
	}
	Location location = new Location(REFERENCE_POINT);
	location.setLatitude(prefs.getFloat(POINT_LATITUDE_KEY, 0));
	location.setLongitude(prefs.getFloat(POINT_LONGITUDE_KEY, 0));
	return location;
    }


}
